package cn.stylefeng.guns.modular.controller;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjAuthentication;
import cn.stylefeng.guns.modular.service.IIdjAuthenticationService;
import cn.stylefeng.guns.modular.util.HttpClientUtil;
import cn.stylefeng.guns.modular.util.HttpSend;
import cn.stylefeng.guns.modular.util.MessageConstant;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * <p>
 * 实名认证表 前端控制器
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-07
 */
@Controller
@RequestMapping("/modular/authentication")
public class IdjAuthenticationController {

	private static String PREFIX = "/authentication/";

	@Autowired
	private IIdjAuthenticationService service;

	/**
	 * 实名认证首页
	 */
	@RequestMapping
	public String index() {
		return PREFIX + "authentication.html";
	}

	@RequestMapping("details/{realAuthenticationId}")
	public ModelAndView add(@PathVariable String realAuthenticationId) {
		ModelAndView index = service.edit(Long.valueOf(realAuthenticationId));
		index.setViewName(PREFIX + "authentication_details.html");
		return index;
	}

	@RequestMapping("/refuse")
	public String refuse(Long realAuthenticationId,String pushId, ModelMap map) {
		map.put("realAuthenticationId", realAuthenticationId);
		map.put("pushId", pushId);
		return PREFIX + "authentication_refuse.html";
	}

	/**
	 * 查询实名认证列表
	 */
	@ResponseBody
	@GetMapping("list")
	public LayuiPageInfo list(String timeLimit, String keyword, Integer idType) {
		// 获取分页参数
		Page<IdjAuthentication> page = LayuiPageFactory.defaultPage();

		// 拼接查询条件
		String beginTime = null;
		String endTime = null;

		if (ToolUtil.isNotEmpty(timeLimit)) {
			String[] split = timeLimit.split(" - ");
			beginTime = split[0];
			endTime = split[1];
		}
		String yes = "yes";
		// 根据条件查询日志
		return service.getList(page, keyword, beginTime, endTime, yes, null, idType);
	}

	/**
	 * 查询实名认证列表
	 */
	@ResponseBody
	@GetMapping("lists")
	public LayuiPageInfo lists(String timeLimits, String keywords, Integer idType) {
		// 获取分页参数
		Page<IdjAuthentication> page = LayuiPageFactory.defaultPage();

		// 拼接查询条件
		String beginTime = null;
		String endTime = null;

		if (ToolUtil.isNotEmpty(timeLimits)) {
			String[] split = timeLimits.split(" - ");
			beginTime = split[0];
			endTime = split[1];
		}
		String no = "no";;
		// 根据条件查询日志
		return service.getList(page, keywords, beginTime, endTime, null, no, idType);
	}

	/**
	 * 审核
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/update")
	@ResponseBody
	public ResponseData update(@RequestBody IdjAuthentication cation) {
		if (ToolUtil.isEmpty(cation.getRealAuthenticationId())) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		cation.setOperationUserId( LoginContextHolder.getContext().getUserId());
		cation.setOperationTime(new Date());
		service.setIsAudit(cation);
		if(ToolUtil.isNotEmpty(cation.getPushId()) && !cation.getPushId().equals("undefined")){
			if(cation.getStatus() == 1){
				HttpSend.send(cation.getPushId(), MessageConstant.TYPE_4, MessageConstant.TITLE_4);
			}else if(cation.getStatus() == 2){
				HttpSend.send(cation.getPushId(), MessageConstant.TYPE_5, MessageConstant.TITLE_5);
			}
		}
		return ResponseData.success();
	}
}

